import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.nio.ByteBuffer;

/******************************************************************************************************************
* File:SystemSuperClass.java
*
* Description:
*
* This class serves as the superclass for all 3 systems A, B and C. It implements a method that is required by
* all 3 systems to determine the frame length of a source file in bytes.
*
* Parameters: 		none
*
*@author Team 10
*
******************************************************************************************************************/
public class SystemSuperClass
{
  

protected static void determineFrameLength(String filename)
{
 
	    int bytesread = 0;					// Stores the number of bytes read from the input file. Used as a counter
		DataInputStream in = null;			// File stream reference to the file passed by the System Sub classes
		byte[] bytearr = new byte[4];		// 4 byte chunk used to store the value of ID
		int byteIDValue = 0;				// Integer value that corresponds to the ID

		try
		{
			/***********************************************************************************
			*	Here we open the file 
			***********************************************************************************/

			in = new DataInputStream(new FileInputStream(filename));
		
			// skip the first 12 bytes because the first ID will be 0 referring to the timestamp.
			// The goal of the function is to find the second 0 ID at which point the first frame ends
			in.skipBytes(12);
			
			//increment the counter by 12, since we have skipped 12 bytes
			bytesread = bytesread+12;
			
			while(true)
			{
				
				//Read the next 4 bytes which represent the ID
				for(int i=0;i<4;i++)
				{
					bytearr[i] = in.readByte();
					bytesread++;
				}
				// Convert the 4 byte array to Integer to determine the value of ID
				byteIDValue = ByteBuffer.wrap(bytearr).getInt();
				
				//If the value is 0 then we have found the beginning of the second frame
				if(byteIDValue == 0)
				{
					//backtrack since we are already 4 bytes into the second frame, and break from the loop
					bytesread = bytesread - 4;
					break;
				}
				
        // If ID doesn't match then the flow ends up here, where it increments by 8 bytes to skip the data frame and
		// get the next ID frame
		in.skipBytes(8);
  		bytesread = bytesread+8;
					
	

			} // while
			
		
			// sets the value of frame length which will be used by the System sub classes
			Measurement.framelength = bytesread;
			
		} 
		catch (FileNotFoundException e){
			//Message to the user if a non-existent file name is passed as parameter
			System.out.println("File not found. Please make sure the Input file is in the current directory or provide a full path to it");
			System.exit(1);
		}
		catch (ArrayIndexOutOfBoundsException e){
		
			System.out.println("Correct Usage: java <SystemA | SystemB | System C> <sourcefile> [sourcefile2]");
			System.exit(1);
		}
		catch (Exception e)
		{
			System.out.println(e);
		}
		
		
 }
}